/**
 * 
 */
package edu.neelsys.mvc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.neelsys.business.search.SearchService;
import edu.neelsys.mvc.util.Constants;
import edu.neelsys.mvc.util.ErrorMessage;
import edu.neelsys.objectmodel.dbentites.Address;
import edu.neelsys.objectmodel.queryviews.CollegeSearchDTO;

/**
 * @author Neel Sys
 * 
 */
@Controller
@RequestMapping("/collegeSearch.html")
public class CollegeSearchController {

	private SearchService searchService;

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		String userID = request.getParameter("city");

		Address address = new Address();
		address.setCity(request.getParameter("city"));
		address.setCountry(request.getParameter("country"));
		address.setAddress(request.getParameter("address"));

		List<CollegeSearchDTO> colleges = searchService.searchCollege(address,
				request.getParameter("courseName"));

		String errorMessage = ErrorMessage.message;
		ErrorMessage.message = null;
		if (errorMessage != null) {

			ModelAndView mav = new ModelAndView(Constants.COLLEGE_SEARCH_VIEW,
					Constants.COLLEGE_SEARCH_FORM_MODEL, colleges);
			mav.addObject(errors);
			return mav;
		} else {
			

			ModelAndView successmav = new ModelAndView(
					Constants.COLLEGE_SEARCH_VIEW,
					Constants.COLLEGE_SEARCH_FORM_MODEL, colleges);
			successmav.addObject(colleges);
			return successmav;
		}
	}

}
